package com.tanhua.admin.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.admin.mapper.UserMapper;
import com.tanhua.admin.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;


/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 19:19 2021/1/4
 * @ModifiedBy:
 */
@Service
@Slf4j
public class UserService {
    @Value("${tanhua.sso.url}")
    private String ssoUrl;

    @Autowired
    private RestTemplate restTemplate;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    private UserMapper userMapper;

    /**
     * @Description: 通过sso的接口进行查询
     * @Param:[token]
     * @return:com.tanhua.server.pojo.User
     * @Author:JinboYang
     * @Date:2021/1/4
     */
    public User queryUserByToken(String token) {
        String url = ssoUrl + "/user/" + token;
        try {
            String data = restTemplate.getForObject(url, String.class);
            if (StringUtils.isEmpty(data)) {
                return null;
            }
            //反序列化成对象返回
            return MAPPER.readValue(data, User.class);
        } catch (Exception e) {
            log.error("校验token出错", e);
        }
        return null;
    }


    /**
     * @Description: 新增用户
     * @Param:[]
     * @return:java.util.List<java.lang.Integer>
     * @Author:JinboYang
     * @Date:2021/1/17
     */
    public List<Integer> saveUser(List<Long> thisYearTimeList, List<Long> lastYearTimeList) {
        List<Integer> list = new ArrayList<>();
        //遍历集合开始找到这个时间段的所有用户
        for (int i = 0; i < thisYearTimeList.size() - 1; i++) {
            LambdaQueryWrapper<User> thisYearQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<User> lastYearQueryWrapper = new LambdaQueryWrapper<>();
            thisYearQueryWrapper.ge(User::getCreated, new Date(lastYearTimeList.get(i))).lt(User::getCreated, new Date(lastYearTimeList.get(i + 1)));
            lastYearQueryWrapper.ge(User::getCreated, new Date(thisYearTimeList.get(i))).lt(User::getCreated, new Date(thisYearTimeList.get(i + 1)));
            Integer thisYearCount = this.userMapper.selectCount(thisYearQueryWrapper);
            Integer lastYearCount = this.userMapper.selectCount(lastYearQueryWrapper);
            list.add(thisYearCount);
            list.add(lastYearCount);
        }
        return list;
    }

    /**
     * @Description: 次日留存率
     * @Param:[thisYearTimeList, lastYearTimeList]
     * @return:java.util.List<java.util.List<java.lang.Long>>
     * @Author:JinboYang
     * @Date:2021/1/18
     */
    public List<List<Long>> queryNextDay(List<Long> thisYearTimeList, List<Long> lastYearTimeList) {

        List<List<Long>> nextList = new ArrayList<>();
        //查询时间
        for (int i = 0; i < thisYearTimeList.size() - 1; i++) {
            LambdaQueryWrapper<User> thisYearWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<User> lastYearWrapper = new LambdaQueryWrapper<>();

            thisYearWrapper.between(User::getId, new Date(thisYearTimeList.get(i) - 24 * 3600000), new Date(thisYearTimeList.get(i + 1) - 24 * 3600000));
            lastYearWrapper.between(User::getId, new Date(lastYearTimeList.get(i) - 24 * 3600000), new Date(lastYearTimeList.get(i + 1) - 24 * 3600000));

            List<User> thisYearUserList = this.userMapper.selectList(thisYearWrapper);
            List<User> lastYearUserList = this.userMapper.selectList(lastYearWrapper);


            List<Long> thisYearUserIds = new ArrayList<>();
            for (User user : thisYearUserList) {
                thisYearUserIds.add(user.getId());
            }
            List<Long> lastYearUserIds = new ArrayList<>();
            for (User user : lastYearUserList) {
                lastYearUserIds.add(user.getId());
            }
            //添加进集合
            nextList.add(thisYearUserIds);
            nextList.add(lastYearUserIds);

        }
        return nextList;
    }


    public Integer queryCumulativeUsers() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        return this.userMapper.selectCount(wrapper);
    }

    public Integer queryUsersByTime(DateTime zeroTime, DateTime nowTime) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(User::getCreated, zeroTime, nowTime);
        return this.userMapper.selectCount(wrapper);
    }

    /**
     * @Description: 根据创建时间查询注册用户
     * @Param:[startDate, endDate]
     * @return:java.util.List<com.tanhua.admin.pojo.User>
     * @Author:JinboYang
     * @Date:2021/1/18
     */
    public List<User> queryUserListByTime(Long startDate, Long endDate) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(User::getCreated, new Date(startDate), new Date(endDate));
        return this.userMapper.selectList(wrapper);
    }
}
